package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.domain.hdw.Agency;
import cc.yugu.yhdw.common.domain.hdw.AgentHdwSetting;
import cc.yugu.yhdw.common.tool.memcached.MemCachedConfig;
import com.whalin.MemCached.MemCachedClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 运营商设置业务切面类
 * 
 * @Date:Created in 2018/7/5 16:24
 * @Author:
 * @Modified By: zjn 2018/7/5 16:24
 */ 
@Component
@Aspect
public class AgentHdwSettingServiceAspect {

    @Autowired
    MemCachedClient memCachedClient;

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.AgentHdwSettingService.update(..))")
    private void update() {}
    /**
    * 更新
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/5  16:25
    */
    @Around("update()")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        AgentHdwSetting agentSetting = (AgentHdwSetting) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_AGENT_HDW_SETTING, agentSetting.getId());
        memCachedClient.delete(key);
        return result;
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.AgentHdwSettingService.delete(..))")
    private void delete() {}
    /**
    * 删除
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/5  16:25
    */
    @Around("delete()")
    public Object delete(ProceedingJoinPoint joinPoint) throws Throwable {
        Integer id = (Integer) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_AGENT_HDW_SETTING, id);

        Integer result = (Integer)joinPoint.proceed();
        if(result != null && result > 0) {
            memCachedClient.delete(key);
        }
        return result;
    }
}
